(function (doc, win) {
    var controller = null;
    var tempParamData = null;
    // sui 页面就绪时间
    $(doc).on("pageInit", "#addEmployeePage", function (e, pageId, $page, isBack) {
        var user = sessionStorageClient.getJSON("user");
        if (isBack) {
            $('#addEmployeePage  #shopId').val(employeeStoreListFilterParam.shopName);
        } else {
            //默认进入该页面,默认清空页面信息;
            $("#addEmployeePage .form-wrapper>ul>li>input").val("");
            $("#addEmployeePage .form-wrapper>ul>li>i").css("display", "none");
            if (!employeeFilterParam.inited) {
                //获取门店数据
                new EmployeeFilterController().getShopList({}, null, false);
            }
            CommonUtil.render($("#userAddContainer"), "userAddTPL", { data: employeeFilterParam });
            CommonUtil.render($("#userAddContainer"), "userAddTPL", { role: user.role });
            //获取临时对象信息;
            tempParamData = CommonUtil.takeOutParamObject();
            if (tempParamData && tempParamData != null) {
                controller = new AddEmployeeController();
            } else {
                DialogUtil.dialogWarn("参数异常,无法创建员工!");
                return false;
            }
            var rg_data = [{
                text: "总部",
                value: 1
            }, {
                text: "单门店",
                value: 2
            },
            ];
            var er_data = []
            if (user.role == 4) {
                er_data = [{
                    text: "服务员",
                    value: 5
                }, {
                    text: "收银员",
                    value: 6
                }
                ];
                $("#addEmployeePage #employeeLevel").attr("data-employeeLevelId", 5);
                $("#addEmployeePage #employeeLevel").val("5");//默认:服务员 
                $("#addEmployeePage #affiliation").val("2");//默认:单门店      
            } else {
                er_data = [{
                    text: "运营/市场",
                    value: 2
                }, {
                    text: "财务",
                    value: 3
                },
                ];

                $("#addEmployeePage #employeeLevel").attr("data-employeeLevelId", 2);
                $("#addEmployeePage #employeeLevel").val("2");//默认:运营/市场
                $("#addEmployeePage #affiliation").val("1");//默认:总部
            }
            $('#rganization').mobiscroll().select({
                theme: 'ios',
                display: 'bottom',
                lang: 'zh',
                placeholder: '总部',
                data: rg_data,
                onSelect: function (valueText, inst) {
                    $(this).siblings("input[type=hidden]").val(inst._tempValue);
                    if (valueText === "单门店") {
                        $("#addEmployeePage #employeeLevel").attr("data-employeeLevelId", 4);
                        $("#addEmployeePage .showShop").removeClass("hide");
                        er_data = [{
                            text: "店长",
                            value: 4
                        }, {
                            text: "服务员",
                            value: 5
                        }, {
                            text: "收银员",
                            value: 6
                        }
                        ];
                        er.option({ data: er_data });
                        er.refresh();
                    } else if (valueText === "总部") {
                        $("#addEmployeePage .showShop").addClass('hide');
                        er_data = [{
                            text: "运营/市场",
                            value: 2
                        }, {
                            text: "财务",
                            value: 3
                        },
                        ];
                        er.option({ data: er_data });
                        er.refresh();
                    }
                    er_data.forEach(function (val) {
                        if (val.text === er._tempValue) {
                            $('#addEmployeePage #employeeLevel').val(val.value);
                        }
                    })
                }

            });
          
            var er;
            $('#employee-role').mobiscroll().select({
                theme: 'ios',
                display: 'bottom',
                lang: 'zh',
                data: er_data,
                placeholder: user.role == 4 ? '服务员' : '运营/市场',
                defaultValue: user.role == 4 ? '服务员' : '运营/市场',

                onInit: function (event, inst) {
                    er = event;
                },
                onSelect: function (valueText, inst) {
                    $(this).siblings("input[type=hidden]").val(inst._tempValue);
                }
            });

            var ws_data = [{
                text: "在职",
                value: 1
            }, {
                text: "离职",
                value: 2,
                disabled: true
            },
            ];
            $('#work-status').mobiscroll().select({
                theme: 'ios',
                display: 'bottom',
                lang: 'zh',
                data: ws_data,
                placeholder: '请选择',
                onSelect: function (valueText, inst) {
                    $(this).siblings("input[type=hidden]").val(inst._tempValue);
                }
            });
            $("input.mbsc-control").val("");
        }
    });


    //绑定新增员工事件;
    $(doc).on("click", "#addEmployeePage a[name='sub_add']", function () {
        if ($('#addEmployeePage .btn').attr("class") == "btn disabled") {
            return;
        }
        if (tempParamData && tempParamData != null) {
            controller.addEmployee();
        } else {
            DialogUtil.dialogWarn("参数异常,无法创建员工!");
            return false;
        }

    });

    $(doc).on('input ', '#addEmployeePage .form>li>input', function () {
        if ($(this).parents('li').siblings().find('input').val() != '' && $(this).val() != '') {
            $('.btn').removeClass('disabled')
        }
        else {
            $('.btn').addClass('disabled')
        }
    });

    $(doc).on('clearInput ', '#addEmployeePage .icon-ico_shutdown', function () {
        for (var i = 0; i < $('.form li input').length; i++) {
            if ($('.form li input').eq(i).val() == '') {
                $('.btn').addClass('disabled')
            }
        }
    });
    //门店筛选页面
    $(doc).on("click", "#addEmployeePage a[name='select_store']", function () {
        $.router.load("employee_store_list.html");
    });

})(document, window);